{% extends 'layout/basic.html' %}
{% load static %}
{% block title %}用户注册{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="accout">
        <div class="title">用户注册</div>
        <form id="regform" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>

                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>

                            <div class="col-xs-5">
                                <input type="button" class="btn btn-default" value="点击获取验证码 " id="btnSms" ></div>
                        </div>

                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {#                    <input type="email" class="form-control" id="inputEmail3" placeholder="Email">#}
                        {{ field }}
                         <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div class="row">
                <div class="col-xs-3">
                    <input type="button" class="btn  btn-primary" value="注册" id="msg_submit">
                </div>
            </div>

        </form>
    </div>
{% endblock %}


{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            msgSubmit();


        });

        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty();
                // 获取用户输入的手机号
                var mobilePhone = $('#id_mobile_phone').val();
                $.ajax({
                    url: '/send_sms/',
                    type: "GET",
                    data: {'mobile_phone': mobilePhone, 'tpl': 'register', },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            sendSmsRemind();

                        } else {

                            $.each(res.error, function (key, value) {

                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }
        //倒计时
        function sendSmsRemind(){
            var $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled',true);   //禁用
            var time = 60;
            var remind =  setInterval(function (){
                $smsBtn.val(time+'秒重新发送');
                time = time-1;
                if(time < 1){
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled',false);

                }
            },1000)
        }
        function msgSubmit(){
            $('#msg_submit').click(function (){
                $('.error-msg').empty();
                // 收集表单中的数据，找到每一个字段

                // 数据ajax发送到后台
                $.ajax({
                    url: '/register/',
                    type: 'POST',
                    dataType: 'JSON',
                    data: $('#regform').serialize(),  // 所有字段的数据 + csrf_token,
                    success: function (arg){
                        console.log(arg);
                    }
                })

            })
        }
    </script>

{% endblock %}